<?php

namespace App\Http\Controllers\Api;

use App\Enums\CodeMode;
use App\Http\Controllers\Controller;
use App\Http\Requests\SMSCaptchaRequest;
use App\Jobs\SendEmailJobs;
use App\Services\AliDySms;
use App\Services\SystemConfig;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\Cache;
use Ugly\Base\Traits\ApiResource;
use Illuminate\Http\Request;

class CaptchaController extends Controller
{
    use ApiResource;

    // 图片验证码
    public function image(): JsonResponse
    {
        return $this->success(app('captcha')->create('math', true));
    }

    // 短信验证码
    public function sms(SMSCaptchaRequest $request): JsonResponse
    {
        $tel = $request->input('tel');
        $captcha = Cache::get('sms_captcha_'.$tel);
        if ($captcha) {
            return $this->failed('请勿重复发送！');
        }
        $captcha = rand(1000, 9999);
        $code_mode = SystemConfig::get('send_code_mode');

        if ($code_mode == CodeMode::SMS->value) {
            // 短信验证码
            (new AliDySms)->captcha($captcha, $tel);
        } elseif ($code_mode == CodeMode::Email->value) {
            // 邮箱验证码
            SendEmailJobs::dispatch($tel, $captcha);
        }

        Cache::put('sms_captcha_'.$tel, $captcha, now()->addMinute());

        return $this->success([]);
    }
    /**
     * 获取验证码
     *
     * @param Request $request
     * @return void
     */
    public function ajcaptcha(Request $request)
    {
        $captchaType = $request->get('captchaType', 'clickWord');
        if (!$captchaType) {
            return $this->fail('请输入类型');
        }

        return $this->success(aj_captcha_create($captchaType));
    }
    /**
     * 验证验证码是否正确
     *
     * @return void
     */
    public function ajcheck(Request $request)
    {
        $token = $request->post('token', '');
        $pointJson = $request->post('pointJson', '');
        $captchaType = $request->post('captchaType', 'clickWord');
        try {
            aj_captcha_check_one($captchaType, $token, $pointJson);
            return app('json')->success();
        } catch (\Throwable $e) {
            return app('json')->fail($e->getMessage());
        }
    }
}
